#!/usr/bin/python
# -*-coding:utf-8-*-
# pip install ddddocr 图片识别
"""天眼守望"""
import math
import time
import re
import requests
import json
from bs4 import BeautifulSoup
from datetime import datetime
from time import sleep
import common
import db
import pymongo
import sys
import random
from dotenv import load_dotenv, find_dotenv
import os
import asyncio
from pyppeteer import launch
import numpy as np
import pandas as pd
import os

mysqldb = db.DbManager()

load_dotenv(verbose=True)
# 今天
today_time = common.TodayTime()
# 将-替换为.
today_time_new = today_time.replace('-', '.')
# 账号
username = os.getenv('LOGIN_ADMIN')
passworld = os.getenv('LOGIN_PWD')
url = os.getenv('URL')

# 获取登录后cookie
async def get_cookie(page):
    cookies_list = await page.cookies()
    cookies = ''
    for cookie in cookies_list:
        str_cookie = '{0}={1};'
        str_cookie = str_cookie.format(cookie.get('name'), cookie.get('value'))
        cookies += str_cookie
    print(cookies)
    return cookies


async def login():
    log_url = url
    # userDataDir:记录上网记录 是否已登录等。。 userDataDir='./userdata',
    # browser = await launch(headless=True, userDataDir='./userdata',args=['--no-sandbox'])
    browser = await launch(headless=True,userDataDir='./userdata', args=['--disable-infobars', '--no-sandbox'])
    page = await browser.newPage()
    await page.setUserAgent(
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36 LBBROWSER")
    await page.setViewport(viewport={'width': 1536, 'height': 768})
    await page.evaluateOnNewDocument('() =>{ Object.defineProperties(navigator,'
                                     '{ webdriver:{ get: () => false } }) }')

    await page.goto(log_url)
    # 查看title 是否等于
    login_text = await page.querySelectorAllEval('#btnLogin', 'nodes=>nodes.map(node=>node.innerText)')
    print(str(login_text))
    print("到这边")
    if "登" in str(login_text[0]):
        # 点击登录
        await page.type(
            '#accountName',
            username)
        await page.type(
            '#accountPwd',
            passworld, {'delay': 120})
        # 识别图片二维码
        print("开始识别图片验证码")
        # 标签截图 不删
        img = await page.querySelector('#verifyCode')
        await img.screenshot(path='CODE_IMG.png')
        import ddddocr
        ocr = ddddocr.DdddOcr(old=True)

        with open("CODE_IMG.png", 'rb') as f:
            image = f.read()
        # 识别后的验证码
        res = ocr.classification(image)
        # 替换 res
        if "u" in res:
            res = res.replace('u', '5')
        if "o" in res:
            res = res.replace('o', '0')
        if "l" in res:
            res = res.replace('l', '1')
        if "i" in res:
            res = res.replace('i', '1')
        print('识别出的验证码为：' + res)
        sleep(0.5)
        await page.type('#code', res, {'delay': 100})
        sleep(1)
        # 点击登录
        await page.click("#btnLogin")
        sleep(0.5)
        sleep(1)
        while True:
            try:
                login_text = await page.querySelectorAllEval('#btnLogin', 'nodes=>nodes.map(node=>node.innerText)')
            except Exception as e:
                print("验证码识别成功.")
                break
            if "登" in str(login_text[0]):
                print("验证码验证失败,刷新验证码")
                await page.evaluate('document.querySelector("#code").value=""')
                await page.click("#verifyCode")
                await img.screenshot(path='CODE_IMG.png')
                with open("CODE_IMG.png", 'rb') as f:
                    image = f.read()
                # 识别后的验证码
                res = ocr.classification(image)
                # 替换 res
                if "u" in res:
                    res = res.replace('u', '5')
                if "o" in res:
                    res = res.replace('o', '0')
                if "l" in res:
                    res = res.replace('l', '1')
                if "i" in res:
                    res = res.replace('i', '1')
                print('识别出的验证码为：' + res)
                sleep(0.5)
                await page.type('#code', res, {'delay': 100})
                # 点击登录
                await page.click("#btnLogin")
                sleep(1)
            else:
                print("验证码识别成功,跳出循环")
                break
    await page.goto("http://58.49.122.254:6888/tysw/index.html")
    sleep(3)
    # 获取js cookile
    js_page_cookie = await page.evaluate("""
                                            function get_cookie(){
                                            return document.cookie}
                                            """)
    # 获取并保存cookies
    page_cookie = await page.cookies()
    cookies = ''
    for cookie in page_cookie:
        str_cookie = '{0}={1};'
        str_cookie = str_cookie.format(cookie.get('name'), cookie.get('value'))
        cookies += str_cookie
    new_cookies = js_page_cookie + ";" + cookies
    print(new_cookies)
    if 'AccessTokenForWeb' in js_page_cookie:
        arr_token = js_page_cookie.split('AccessTokenForWeb=')
        token = arr_token[1].split('; ')
    # List = {'cookie': js_page_cookie, 'token': token[0]}
    # with open("./cookie.txt", 'w') as file:
    #     [file.write("{}&&".format(List[i])) for i in List]


    await page.click("body > div > div.leftMenuWrap > div.leftMenuCoat > div > ul > li:nth-child(7)")
    sleep(1)
    await page.click("body > div > div.leftMenuWrap > div.leftMenuCoat > div > ul > ul:nth-child(8) > li > a")
    sleep(5)
    list_tr = await page.querySelectorAllEval(
        '#reportListGrid>tbody>tr',
        'nodes=>nodes.map(node=>node.innerHTML)')
    print(len(list_tr))
    for i in range(len(list_tr)+1):
        if i<=1:
            continue
        print("第" + str(i) + "行")
        # orgId_arr = await page.querySelectorAllEval(
        #     '#reportListGrid > tbody > tr:nth-child(' + str(i) + ') > td:nth-child(11)',
        #     'nodes=>nodes.map(node=>node.innerHTML)')
        reportName=await page.querySelectorAllEval(
            '#reportListGrid>tbody>tr:nth-child('+str(i)+')>td:nth-child(7)',
            'nodes=>nodes.map(node=>node.innerText)')
        print(reportName)
        reportDataTypeName = await page.querySelectorAllEval(
            '#reportListGrid>tbody>tr:nth-child(' + str(i) + ')>td:nth-child(10)',
            'nodes=>nodes.map(node=>node.innerText)')
        reportTime = await page.querySelectorAllEval(
            '#reportListGrid>tbody>tr:nth-child(' + str(i) + ')>td:nth-child(11)',
            'nodes=>nodes.map(node=>node.innerText)')
        regionName = await page.querySelectorAllEval(
            '#reportListGrid>tbody>tr:nth-child(' + str(i) + ')>td:nth-child(12)',
            'nodes=>nodes.map(node=>node.innerText)')
        cropName = await page.querySelectorAllEval(
            '#reportListGrid>tbody>tr:nth-child(' + str(i) + ')>td:nth-child(13)',
            'nodes=>nodes.map(node=>node.innerText)')
        creatorName = await page.querySelectorAllEval(
            '#reportListGrid>tbody>tr:nth-child(' + str(i) + ')>td:nth-child(14)',
            'nodes=>nodes.map(node=>node.innerText)')
        createTime = await page.querySelectorAllEval(
            '#reportListGrid>tbody>tr:nth-child(' + str(i) + ')>td:nth-child(15)',
            'nodes=>nodes.map(node=>node.innerText)')
        publishStatus = await page.querySelectorAllEval(
            '#reportListGrid>tbody>tr:nth-child(' + str(i) + ')>td:nth-child(16)>span',
            'nodes=>nodes.map(node=>node.innerText)')
        # #\31  > td:nth-child(3)
        reportId = await page.querySelectorAllEval(
            '#reportListGrid>tbody>tr:nth-child(' + str(i) + ')>td:nth-child(3)',
            'nodes=>nodes.map(node=>node.innerText)')
        reportId=reportId[0]
        info = {"reportName": reportName[0].replace("\t", ""),
                "reportTypeName": "观点报告",
                "reportDataTypeName": reportDataTypeName[0].replace("\t", ""),
                'reportTime': reportTime[0].replace("\t", ""),
                'regionName': regionName[0].replace("\t", ""),
                'cropName': cropName[0].replace("\t", ""),
                'creatorName': creatorName[0].replace("\t", ""),
                'createTime': createTime[0].replace("\t", ""),
                'publishStatus': publishStatus[0].replace("\t", ""),
                'reportId': reportId,
                # http://58.49.122.254:6888/jh-cloud/report/download?reportId=192&type=pdf&AccessToken=d27f70add55c033be6ac22210c298d69
                'filePath': "http://58.49.122.254:6888/jh-cloud/report/download?reportId="+str(reportId)+"&type=pdf&AccessToken="+token[0],
                'url':"http://58.49.122.254:6888/jh-cloud/report/download?reportId="+str(reportId)+"&type=pdf&AccessToken=",
                'token':token[0],
                }
        now_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        info['create_date'] = now_time
        find_info = mysqldb.table_select_one(table="report_info", field="id", where={"reportId": reportId})
        # 存在数据则跳过
        if find_info:
            info['update_date'] = now_time
            mysqldb.table_update(table="report_info", data=info,
                                 where={"reportId": reportId})
        else:
            mysqldb.table_insert(table="report_info", data=info)

    # 更新所有数据token.
    now_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    info_token={
        'token':token[0],
        'update_date':now_time
    }
    mysqldb.table_update(table="report_info", data=info_token,
                                 where="reportId>=1")
    # 关闭
    await page.close()
    await browser.close()


class Egas:
    def __init__(self):
        self.url = url
        self.main()

    def main(self):
        # 登录
        asyncio.get_event_loop().run_until_complete(login())

Egas()
